package util;

/**
 * @author lyl
 * description:
 */

import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;

/**
 * <dependency>
 * <groupId>net.coobird</groupId>
 * <artifactId>thumbnailator</artifactId>
 * <version>0.4.8</version>
 * </dependency>
 * 谷歌图片压缩
 */

public class GoogleCompress {

//    private static final String basePath = "d:/img/";
//
//    public static void main(String[] args) {
//        File[] files = getFileList(basePath);
//        for (File file : files) {
//            System.out.println(file.getAbsolutePath());
//            googleCompress(file.getAbsolutePath(), "D:\\picture\\" + file.getName() + "_test" + file.getName().substring(file.getName().indexOf(".")));
//        }
//    }

    public static void googleCompress(String olderImg, String newImg) {
        try {
            /**
             * scale图片长宽  outputQuality图片质量
             */
            Thumbnails.of(olderImg).scale(0.5f).outputQuality(0.5f).toFile(newImg);
            /**
             * 指定大小
             */
            //Thumbnails.of(olderImg).size(300, 300).toFile(newImg);
            /**
             * keepAspectRatio(false)按比例
             */
            //Thumbnails.of(olderImg).size(120, 120).keepAspectRatio(false).toFile(newImg);
            /**
             * rotate(角度),正数：顺时针 负数：逆时针
             */
            // Thumbnails.of(olderImg).size(1280, 1024).rotate(90).toFile(newImg);
            /**
             *  watermark(位置，水印图，透明度)
             */
//            Thumbnails.of(olderImg).size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File(olderImg)), 0.5f).outputQuality(0.8f).toFile(newImg);
            /**
             * 裁剪
             */
            //  Thumbnails.of(olderImg).sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile(newImg);
            /**
             * 转换图像格式
             */
            // Thumbnails.of(olderImg).size(1280, 1024).outputFormat("png").toFile(newImg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static File[] getFileList(String path) {
        File file = new File(path);
        File[] filelist = null;
        if (file.exists()) {
            filelist = file.listFiles();
        }
        return filelist;
    }

}

